<?php

function introducirUsuarioFacebook($user_profile) {
    $conn = conectarDb();
    $idFacebook = $user_profile["id"];
    $usuario = $user_profile["name"];
    $clave = $user_profile["id"];
    $nombre = $user_profile["first_name"];
    $apellido = $user_profile["last_name"];
    $pais = $user_profile["hometown"]["name"];
    $sql = "INSERT into " . $GLOBALS["databaseNombre"] . ".user (id_facebook,username,password, last_visit, firstname, lastname, country) VALUES
 ('$idFacebook','$usuario', md5('$clave'),now(),'$nombre','$apellido','$pais')";
    switch ($GLOBALS["database"]) {
        case 1:
            $result = mysql_query($sql) or die(mysql_error());
            break;
        case 2:
            //  $result = pg_query($sql);
            break;
    }
    return $result;
}

function loginFacebook($user_profile) {
    $conn = conectarDb();
    $usuario = $user_profile["name"];
    $clave = $user_profile["id"];
    $sql = "select * from " . $GLOBALS["databaseNombre"] . ".user where username='$usuario' and password=md5('$clave')";

    switch ($GLOBALS["database"]) {
        case 1:
            $result = mysql_query($sql);
            $lineas = mysql_num_rows($result);
            break;
        case 2:
            $result = pg_query($sql);
            $lineas = pg_num_rows($result);
            break;
    }
    if ($lineas > 0) {
        return true;
    } else {
        return false;
    }
}

function obtenerIdUsuarioFacebook($user_profile) {
    $conn = conectarDb();
    $usuario = $user_profile["name"];
    $sql = "select id_user from " . $GLOBALS["databaseNombre"] . ".user where username='$usuario'";
    switch ($GLOBALS["database"]) {
        case 1:
            $result = mysql_query($sql);
            $linea = mysql_fetch_array($result, MYSQL_ASSOC);
            break;
        case 2:
            $result = pg_query($sql);
            $linea = pg_fetch_array($result, NULL, PGSQL_ASSOC);
            break;
    }
    return $linea['id_user'];
}

function esUsuarioFacebook($id) {
    $conn = conectarDb();
    $sql = "select * from " . $GLOBALS["databaseNombre"] . ".user where id_user='$id' and id_facebook>0";

    switch ($GLOBALS["database"]) {
        case 1:
            $result = mysql_query($sql) or die(mysql_error());
            $lineas = mysql_num_rows($result);
            break;
        case 2:
            $result = pg_query($sql);
            $lineas = pg_num_rows($result);
            break;
    }
    if ($lineas > 0) {
        return true;
    } else {
        return false;
    }
}

function obtenerImagenPerfilFacebook($id) {
    $conn = conectarDb();
    $sql = "select id_facebook from " . $GLOBALS["databaseNombre"] . ".user where id_user='$id'";
    switch ($GLOBALS["database"]) {
        case 1:
            $result = mysql_query($sql);
            $linea = mysql_fetch_array($result, MYSQL_ASSOC);
            break;
        case 2:
            $result = pg_query($sql);
            $linea = pg_fetch_array($result, NULL, PGSQL_ASSOC);
            break;
    }
    $idFacebook=$linea['id_facebook'];
    ?>
    <img src="https://graph.facebook.com/<?php echo $idFacebook ?>/picture">
    <?php
}

?>
